package com.example.text;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class HuanSuan extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huan_suan);
        Button button=findViewById(R.id.Return);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Button button1=findViewById(R.id.erjinzhi);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText= (EditText) findViewById(R.id.shuru);
                String str=editText.getText().toString().trim();
                if(str.isEmpty()){
                    EditText editText2 = findViewById(R.id.shuru);
                    editText2.setText("别空着");
                }
                else {
                    boolean judge=true;
                    for (int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7'&&
                                num!='8'&&num!='9')
                        {
                            TextView textView = findViewById(R.id.getresult);
                            textView.setText("十进制只有0到9");
                            judge = false;
                            break;
                        }
                    }
                    if(judge)
                        {
                        int n = Integer.valueOf(str);
                        TextView textView = findViewById(R.id.getresult);
                        String str2 = Integer.toBinaryString(n);
                        textView.setText("结果：" + str2);
                    }
                    else
                    {
                        EditText editText3 = findViewById(R.id.shuru);
                        editText3.setText("");
                    }
                }
            }
        });

        Button button2=findViewById(R.id.shiliujinzhi);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText= (EditText) findViewById(R.id.shuru);
                String str=editText.getText().toString().trim();
                if(str.isEmpty()){
                    EditText editText2 = findViewById(R.id.shuru);
                    editText2.setText("别空着");
                }
                else {
                    boolean judge=true;
                    for (int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7'&&
                                num!='8'&&num!='9')
                        {
                            TextView textView = findViewById(R.id.getresult);
                            textView.setText("十进制只有0到9");
                            judge = false;
                            break;
                        }
                    }
                    if(judge) {
                        int n = Integer.valueOf(str);
                        TextView textView = findViewById(R.id.getresult);
                        String str2 = Integer.toString(n, 16);
                        textView.setText("结果：" + str2);
                    }
                    else
                    {
                        EditText editText3 = findViewById(R.id.shuru);
                        editText3.setText("");
                    }
                }
            }
        });

        Button button3=findViewById(R.id.bajinzhi);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1= (EditText) findViewById(R.id.shuru);
                String str=editText1.getText().toString().trim();
                if(str.isEmpty()){
                    EditText editText2 = findViewById(R.id.shuru);
                    editText2.setText("别空着");
                }
                else {
                    boolean judge=true;
                    for (int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7'&&
                                num!='8'&&num!='9')
                        {
                            TextView textView = findViewById(R.id.getresult);
                            textView.setText("十进制只有0到9");
                            judge = false;
                            break;
                        }
                    }
                    if(judge) {
                        int n = Integer.valueOf(str);
                        TextView textView = findViewById(R.id.getresult);
                        String str2 = Long.toString(n, 8);
                        textView.setText("结果：" + str2);
                    }
                    else{
                        EditText editText3 = findViewById(R.id.shuru);
                        editText3.setText("");
                    }
                }
            }
        });
        Button button4=findViewById(R.id.erzhuanshijinzhi);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1= (EditText) findViewById(R.id.shuru);
                String str=editText1.getText().toString().trim();
                boolean judge=true;
                if(str.isEmpty()){
                    EditText editText = findViewById(R.id.shuru);
                    editText.setText("别空着");
                }
                else {
                    for (int i = 0; i < str.length(); i++) {
                        char num = str.charAt(i);
                        if (num != '0' && num != '1') {
                            TextView textView = findViewById(R.id.getresult);
                            textView.setText("二进制只有0和1");
                            judge = false;
                            break;
                        }
                    }
                    if (judge) {
                        TextView textView = findViewById(R.id.getresult);
                        textView.setText("结果：" + Integer.parseInt(str, 2));
                    } else {
                        EditText editText = findViewById(R.id.shuru);
                        editText.setText("");
                    }
                }

            }
        });

        Button button5=findViewById(R.id.bazhuanshijinzhi);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1= (EditText) findViewById(R.id.shuru);
                String str=editText1.getText().toString().trim();
                if(str.isEmpty()){
                    EditText editText = findViewById(R.id.shuru);
                    editText.setText("别空着");
                }
                else {
                    boolean judge=true;
                    for (int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7')
                        {
                            TextView textView = findViewById(R.id.getresult);
                            textView.setText("八进制只有0到7的数字");
                            judge = false;
                            break;
                        }
                    }
                    if(judge) {
                        TextView textView=findViewById(R.id.getresult);
                        int num = Integer.parseInt(str, 8);
                        textView.setText("结果：" + num);
                    }
                    else
                    {
                        EditText editText = findViewById(R.id.shuru);
                        editText.setText("");
                    }
                }
            }
        });

        Button button6=findViewById(R.id.shiliuzhuanshijinzhi);
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1= (EditText) findViewById(R.id.shuru);
                String str=editText1.getText().toString().trim();
                if(str.isEmpty()){
                    EditText editText = findViewById(R.id.shuru);
                    editText.setText("别空着");
                }
                else {
                    boolean judge=true;
                    for(int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7'&&
                        num!='8'&&num!='9'&&num!='A'&&num!='B'&&num!='C'&&num!='D'&&num!='E'&&num!='F'&&
                                num!='a'&&num!='b'&&num!='c'&&num!='d'&&num!='e'&&num!='f')
                        {
                            TextView textView = findViewById(R.id.getresult);
                            textView.setText("八进制只有0到9和A到F");
                            judge = false;
                            break;
                        }
                    }
                    if(judge) {
                        TextView textView = findViewById(R.id.getresult);
                        textView.setText("结果：" + Integer.parseInt(str, 16));
                    }
                    else
                    {
                        EditText editText = findViewById(R.id.shuru);
                        editText.setText("");
                    }
                }
            }
        });

    }
}